package com.business.dynamic.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.business.common.dto.R;
import com.business.common.enums.BusinessExceptionEnum;
import com.business.common.exception.BusinessException;
import com.business.dynamic.entity.DynamicCollection;
import com.business.dynamic.mapper.DynamicCollectionMapper;
import com.business.dynamic.service.DynamicCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 用户收藏表:业务接口实现类
 *
 * @author chenjiehong
 * @date 2019-12-02 17:55:03
 * @since 1.0
 */
@Service
public class DynamicCollectionServiceImpl implements DynamicCollectionService {

    @Autowired
    private DynamicCollectionMapper dynamicCollectionMapper;


    @Override
    @Transactional
    public Integer add(Integer dynamicId, Integer userId) {
        DynamicCollection dynamicCollection = new DynamicCollection();
        dynamicCollection.setDynamicId(dynamicId);
        dynamicCollection.setUserId(userId);
        Integer count = dynamicCollectionMapper.selectCount(new EntityWrapper<DynamicCollection>().eq("dynamic_id", dynamicCollection.getDynamicId()).eq("user_id", dynamicCollection.getUserId()));
        if (count != 0) {
            throw new BusinessException(BusinessExceptionEnum.COLLECTION_EXIST);
        }

        return dynamicCollectionMapper.insert(dynamicCollection);
    }


    @Override
    @Transactional
    public Integer delete(Integer dynamicId, Integer userId) {
        return  dynamicCollectionMapper.delete(new EntityWrapper<DynamicCollection>().eq("dynamic_id", dynamicId).eq("user_id", userId));
    }

}